﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
public partial class administrator_uc_admin_v_staticnews : WebPageSecurity
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CommonBLL.RegisterCKFinder(CKEditorBody);
            PanelEdit.Visible = false;
            if (!string.IsNullOrEmpty(Request.QueryString["itemid"]))
            {
                LoadDataInfo(int.Parse(Request.QueryString["itemid"].ToString().Trim()));               
            }
        }
    }
    private void LoadDataInfo(int ItemID)
    {
        DataTable dt = new BLL.CmsBLL().StaticNews_ViewItemByID(ItemID);
        if (dt.Rows.Count > 0)
        {
            ltNewsID.Text = dt.Rows[0]["NewsID"].ToString();
            ltTitle.Text = dt.Rows[0]["Title"].ToString();
            hdItemID.Value = dt.Rows[0]["NewsID"].ToString();
            ltBody.Text = dt.Rows[0]["Body"].ToString();             
            ltActive.Text = dt.Rows[0]["_Active"].ToString();
        }
        else
            lblMessageView.Text = "Load dữ liệu lỗi!";
    }   
    protected void btnEditItem_Click(object sender, EventArgs e)
    {
        PanelView.Visible = false;
        PanelEdit.Visible = true;    
        LoadDataInfoEdit(int.Parse("0"+hdItemID.Value));     
    }
    private void LoadDataInfoEdit(int ItemID)
    {
        DataTable dt = new BLL.CmsBLL().StaticNews_ViewItemByID(ItemID);
        if (dt.Rows.Count > 0)
        {            
            txtTitle.Text = dt.Rows[0]["Title"].ToString();
            CKEditorBody.Text = dt.Rows[0]["Body"].ToString();           
            rdActive.SelectedValue = dt.Rows[0]["Active"] + "";
        }
        else
            lblMessageEdit.Text = "Load data failt!";
    }      
    protected void lbtUpdateItem_Click(object sender, EventArgs e)
    {
        try
        {
            new CmsBLL().StaticNews_UpDateItem(int.Parse(hdItemID.Value), txtTitle.Text, CKEditorBody.Text + "", DateTime.Now, "", int.Parse("0" + rdActive.SelectedValue));
            lblMessageEdit.Text = "Cập nhật thành công!";           
        }
        catch (Exception ex)
        {
            lblMessageEdit.Text = "ERROR! " + ex.Message.ToString();
        }
    }
}